package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCarMapper;
import com.sky.service.ShopCarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.util.resources.cldr.nmg.LocaleNames_nmg;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShopCarServiceImpl implements ShopCarService {
    @Autowired
    private ShopCarMapper shopCarMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        LambdaQueryWrapper<ShoppingCart> lam=new LambdaQueryWrapper<>();
        if(shoppingCartDTO.getDishFlavor()!=null){
            lam.eq(ShoppingCart::getDishFlavor,shoppingCartDTO.getDishFlavor());
        }
        if(shoppingCartDTO.getDishId()!=null){
            lam.eq(ShoppingCart::getDishId,shoppingCartDTO.getDishId());
        }
        if(shoppingCartDTO.getSetmealId()!=null){
            lam.eq(ShoppingCart::getSetmealId,shoppingCartDTO.getSetmealId());
        }
        List<ShoppingCart> shoppingCartList = shopCarMapper.selectList(lam);

        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        if(shoppingCartList!=null&&shoppingCartList.size()>0){
            ShoppingCart car=new ShoppingCart();
            car=shoppingCartList.get(0);
            car.setNumber(car.getNumber()+1);
            shopCarMapper.updateById(car);
        }else {
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId!=null){
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
            } else {
                Setmeal setmeal=setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shopCarMapper.insert(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> showShopCar(Long userId) {
        LambdaQueryWrapper<ShoppingCart> lam=new LambdaQueryWrapper<>();
        lam.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCartList = shopCarMapper.selectList(lam);
        return shoppingCartList;
    }

    @Override
    public void reduceCount(ShoppingCartDTO shoppingCartDTO) {
        LambdaQueryWrapper<ShoppingCart> lam=new LambdaQueryWrapper<>();
        if(shoppingCartDTO.getDishFlavor()!=null){
            lam.eq(ShoppingCart::getDishFlavor,shoppingCartDTO.getDishFlavor());
        }
        if(shoppingCartDTO.getDishId()!=null){
            lam.eq(ShoppingCart::getDishId,shoppingCartDTO.getDishId());
        }
        if(shoppingCartDTO.getSetmealId()!=null){
            lam.eq(ShoppingCart::getSetmealId,shoppingCartDTO.getSetmealId());
        }
        List<ShoppingCart> shoppingCartList = shopCarMapper.selectList(lam);
        if(shoppingCartList.size()>0&&shoppingCartList!=null){
            ShoppingCart car=new ShoppingCart();
            car=shoppingCartList.get(0);
            car.setNumber(car.getNumber()-1);
            shopCarMapper.updateById(car);
        }
    }

    @Override
    public void clearShopCar() {
        Long userId=BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lam=new LambdaQueryWrapper<>();
        lam.eq(ShoppingCart::getUserId,userId);
        shopCarMapper.delete(lam);
    }
}
